<#assign module="member"/>

<@override name="header">
</@override>
<@override name="body">

	<div class="row">
	
		<form id="submit_form" action="${ctx}/member/updateEnterprise" method="post">
		<input type="hidden" id="CSRFToken" name="CSRFToken" value="${CSRFToken}"/>
		<input type="hidden" name="id" value="${enterprise.id}">
		<@shiro.hasPermission name='allowsign'>
        	<input type="hidden" name="allowsign" value="true"/>
        </@shiro.hasPermission>
        
		<div class="col-md-4 col-lg-4">
			<div class="panel panel-default">
				<div class="panel-heading">
					填写客户信息
			    </div>
			    <div class="panel-body">
			    
			    	<div class="form-group">
	                    <label control-label">客户类型：企业用户</label>
	                </div>
    				
    				<div class="form-group">
	                    <label>客户头像：</label>
	                    <br/>
		                <img width="150" height="150" <#if enterprise.photo??>src="${ctx}/upload/member/${enterprise.photo}"<#else>src="${ctx}/assets/css/images/member_img.png"</#if> />
	                </div>
		                
		            <div class="form-group">
	                    <label>企业名称：</label>
	                    <input class="form-control" id="name" name="name" value="${enterprise.name?if_exists}" type="text" maxlength="20" required/>
	                </div>
	                <div class="form-group">
	                    <label><em>*</em>所属公司：</label>
	                    <select class="form-control" id="companyId" name="companyId" required>
							<#list companys as company>
	                    		<#if ismain>
	                    			<option value="${company.id}" <#if enterprise.companyId?? && enterprise.companyId==company.id>selected="selected"</#if>>${company.abbreviation?if_exists}</option>
	                    		<#else>
	                    			<#if enterprise.companyId?? && enterprise.companyId==company.id>
	                    				<option value="${company.id}" selected="selected">${company.abbreviation?if_exists}</option>
	                    			</#if>
	                    		</#if>
							</#list>
	                    </select>
	                </div>
	                <div class="form-group">
	                    <label><em>*</em>企业等级：</label>
	                    <select class="form-control" id="grade_id" name="grade_id" required />
							<option value="">--无--</option>
							<#if grades??>
								<#list grades as grade>
									<option value="${grade.id}" <#if enterprise.grade_id?? && enterprise.grade_id==grade.id>selected="selected"</#if>>${grade.name?if_exists}</option>
								</#list>
							</#if>
						</select>
	                </div>
	                
	                <div class="form-group">
	                    <label>企业地址：</label>
	                    <textarea class="form-control" cols="20" id="address" name="address" rows="2">${enterprise.address?if_exists}</textarea>
	                </div>
	                
	                <div class="form-group">
	                    <label>备注：</label>
	                    <textarea class="form-control" cols="20" id="memo" name="memo" rows="2">${enterprise.memo?if_exists}</textarea>
	                </div>
	                
	                <div class="form-group">
	                     <label for="gender">是否允许签单：</label>
                    	<label style="padding-right:20px;">
						  <input <@shiro.lacksPermission name='allowsign'>disabled="disabled"</@shiro.lacksPermission> type="checkbox" name="allowOverdraw" <#if enterprise.allowOverdraw??>${enterprise.allowOverdraw?string('checked="checked"','')}</#if>/>
						</label>
	                </div>
	                
	                <div class="form-group" style="margin-left:10px;">
	                	<@shiro.hasPermission name='memberedit'>
		                <input id="submit_button" type="submit" class="btn btn-primary" value="保存">
		                </@shiro.hasPermission>
		                <input type="button" class="btn btn-default" onclick="window.location.href = '${ctx}/member/elist';" value="返回">
		            </div>
        		</div>
			</div>
		</div>
	
		<div class="col-md-8 col-lg-8">
			<div class="panel panel-default" style="min-height: 833px;">
				<div class="panel-heading">
					关联手机   
			    </div>
			    <div class="form-group" style="margin-top:10px;margin-left:10px;">
		        	<input type="button" id="add-phone" class="btn btn-default" value="添加">
		        </div>
				<table id="phone-table" class="table" style="margin-left:10px;">
					<tr>
						<th width="5%">#</th>
						<th width="20%">手机号</th>
						<th width="20%">姓名</th>
						<th width="20%">车牌号</th>
						<th width="10%">操作</th>
					</tr>
					<#if enterprise.passengers?? && (enterprise.passengers?size>0)>
						<#list enterprise.passengers as pa>
							<tr>
							   <td>${pa_index+1}</td>
							   <td><input type="text" name="passengers[${pa_index}].id" value="${pa.id?if_exists}" hidden/>
							   <input class="form-control" type="text" id="phoneShow_${pa_index}" name="passengers[${pa_index}].phoneShow" value="${pa.phoneShow?if_exists}" />
							   		<div style="display:none">
							   			<input class="form-control phone-input" type="text" id="phone_${pa_index}" name="passengers[${pa_index}].phone" value="${pa.phone?if_exists}" />
							   		</div>
							   </td>
							   <td><input class="form-control" type="text" name="passengers[${pa_index}].name" value="${pa.name?if_exists}" /></td>
							   <td><input class="form-control" type="text" name="passengers[${pa_index}].plateNumber" value="${pa.plateNumber?if_exists}" /></td>
							   <td><a href='javascript:;' class='btn btn-default deletethis'>删除</a><input type="hidden" name="passengers[${pa_index}].id" value="${pa.id?if_exists}"></td>
						    </tr>
						</#list>
					<#else>
						<tr>
						   <td>1</td>
						   <td><input class="form-control phone-input" type="text" name="passengers[0].phone" /></td>
						   <td><input class="form-control" type="text" name="passengers[0].name" /></td>
						   <td><input class="form-control" type="text" name="passengers[0].plateNumber" /></td>
						   <td><a href='javascript:;' class='btn btn-default deletethis'>删除</a></td>
					    </tr>
					</#if>
				</table>
			</div>
		</div>
		</form>
	</div>
	

</@override>
<@override name="footer">
<script type="text/javascript">
	<#if message??>
		$.scojs_message("${message}", $.scojs_message.TYPE_ERROR);
	</#if>
	var phone_num=$(".deletethis").length;
	$(document).ready(function(){
		
		$(document).on('click','.deletethis',function(){
			$(this).parent().parent().remove();
		});
		
		
		$("#add-phone").click(function(){
			$("#phone-table tbody").append("<tr>"+
										   "<td>"+(phone_num+1)+"</td>"+
										   "<td><input class='form-control phone-input' type='text' name='passengers["+phone_num+"].phone'/></td>"+
										   "<td><input class='form-control' type='text' name='passengers["+phone_num+"].name'/></td>"+
										   "<td><input class='form-control' type='text' name='passengers["+phone_num+"].plateNumber'/></td>"+
										   "<td><a href='javascript:;' class='btn btn-default deletethis'>删除</a></td>"+
										   "</tr>");
			phone_num++;
		});
		
		$("#submit_form").submit(function(){
			var isphone=/^\+?\d{3,13}(\-?(\d{6,8}?))?(\-?(\d{4,8}?))*$/; 
			var isRightPhone=true; 
			
			if($(".phone-input").length==0){
				$.scojs_message("至少填写一条关联手机", $.scojs_message.TYPE_ERROR);
				return false;
			}
			
			var thisPhone=null;
			$(".phone-input").each(function(index,element){
				var phone=$(element).val();
				if (!isphone.test(phone)){
					isRightPhone=false;
					thisPhone=element;
					return false;
				}
			});
			
			if(!isRightPhone){
				$.scojs_message("手机号格式不正确", $.scojs_message.TYPE_ERROR);
				if(null!=thisPhone){
					$(thisPhone).focus();
				}
				return false;
			}
			
			$("#submit_button").attr("disabled",true);
			$.post($(this).attr("action"),$(this).serialize(),function(data){
	            if(data.success){
	                $.scojs_message('修改成功', $.scojs_message.TYPE_OK);
	                window.location='${ctx}/member/elist';
	            }
	            else {
	            	$("#submit_button").attr("disabled",false);
	            	$("#CSRFToken").val(data.object);
	            	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	            	//if(data.errorcode=="0"){
					    //if(confirm(data.message+"，要修改该客户吗？")){
							//window.location='${ctx}/member/edit/'+data.longValue;
						//}
	            	//}
	            	//else{
	            		//$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	            	//}
	            }
		    },'json');
			
			return false;
			
		});
		
		$(document).on("change","#companyId",function(){
			$.ajax({
		        url : '${ctx}/member/findByCompany',
		        type:'GET',
		        async:false,
		        dataType : 'json',
		        data : {
		        	"companyId":$(this).val()
		        },
		        success : function(result){
		        	if(result.success) {
	        			$("#grade_id").html('');
		        		if (result.data != null) {
		        			$(result.data).each(function(index,element){
		        				var text="<option value="+result.data[index].id+">"+result.data[index].name+"</option>";
		            			$("#grade_id").append(text);
							 });
		        		} else {
		        			var text="<option>无</option>";
		        			$("#grade_id").append(text);
		        		}
		        	}
		        }
		     });
		});
		
		<#if enterprise.passengers?? && (enterprise.passengers?size>0)>
			<#list enterprise.passengers as pa>
				$(document).on("change","#phoneShow_${pa_index}",function(){
					$("#phone_${pa_index}").val($(this).val());
				});
			</#list>
		</#if>
		
		
	});
</script>
</@override>
<@extends name="../layout.ftl"/>